import json
from pathlib import Path

# ===== 1. 设置主目录路径 =====
root_folder = Path("D:\\yuanbei\\客服问答机器人需求表\\清洗数据\\data_process")  # ← 修改为你的主目录路径

# ===== 2. 递归查找所有名为 result.json 的文件 =====
json_files = []
for folder in root_folder.rglob("train_data3.json"):
    if folder.is_file():
        json_files.append(folder)

print(f"共找到 {len(json_files)} 个名为 train_data3.json 的文件")
print("json_files:", json_files)
# ===== 3. 合并所有文件内容 =====
merged_data = []

for file in json_files:
    try:
        with open(file, "r", encoding="utf-8") as f:
            data = json.load(f)
            # 支持每个文件是列表或单条记录
            if isinstance(data, list):
                merged_data.extend(data)
            else:
                merged_data.append(data)
            print(f"✅ 已读取: {file.relative_to(root_folder)} ({len(data) if isinstance(data, list) else 1} 条)")
    except Exception as e:
        print(f"⚠️ 文件 {file.name} 读取失败: {e}")

# ===== 4. （可选）去重处理 =====
# merged_data = [json.loads(x) for x in {json.dumps(item, ensure_ascii=False) for item in merged_data}]

# ===== 5. 写入最终合并文件 =====
output_file = root_folder / "train.json"
with open(output_file, "w", encoding="utf-8") as f:
    # 写入开头
    f.write("[\n")
    # 写入每个对话项
    for i, item in enumerate(merged_data):
        # 转换为字符串
        item_str = json.dumps(item, ensure_ascii=False)
        # 最后一项不加逗号
        if i == len(merged_data) - 1:
            f.write(f"    {item_str}\n")
        else:
            f.write(f"    {item_str},\n")
    # 写入结尾
    f.write("]")

print(f"\n🎉 合并完成！输出文件: {output_file}")
print(f"共合并 {len(merged_data)} 条记录。")
